import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
// Welcome是Home的子路由
import Users from '../components/user/Users.vue'
//用户列表路由
Vue.use(VueRouter)
    //权限列表路由
import Rights from '../components/power/rights.vue'
import Roles from '../components/power/Roles.vue'
import Params from '../components/goods/Params.vue'
import Cate from '../components/goods/Cate.vue'
import List from '../components/goods/List.vue'
import Add from '../components/goods/children/Add.vue'
import Order from '../components/order/Order.vue'
import report from '../components/report/report.vue'
const routes = [{
            path: '/login',
            name: 'Login',
            component: Login
        }, {
            path: '/',
            redirect: '/login'
        }, {
            path: '/home',
            name: 'Home',
            component: Home,
            redirect: '/welcome',
            children: [{
                    path: '/welcome',
                    component: Welcome,

                }, {
                    path: '/users',
                    component: Users
                }, {
                    path: '/rights',
                    component: Rights
                }, {
                    path: '/roles',
                    component: Roles
                }, {
                    path: '/params',
                    component: Params
                }, {
                    path: '/categories',
                    component: Cate
                }, {
                    path: '/goods',
                    component: List,

                },
                {
                    path: '/goods/add',
                    component: Add,
                },
                {
                    path: '/orders',
                    component: Order,
                }, {
                    path: '/reports',
                    component: report,
                }

            ]
        }

    ]
    //     //添加一个重定向路由规则 {
    // path: '/',
    //     redirect: '/login'
    // }

const router = new VueRouter({
        routes
    })
    //挂载路由导航守卫
router.beforeEach((to, from, next) => {
    //to将要访问的路径
    //from代表从哪个路径跳转而来
    //next是一个函数。表示放行 next('/login')  表示强制跳转

    if (to.path === '/login') return next()
        //如果不是 先获取token
    const toKenStr = window.sessionStorage.getItem('token')
    if (!toKenStr) return next('/login')
    next()

})

export default router